package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.WeekBizService;
import com.neusoft.weekreport.component.entities.*;
import com.neusoft.weekreport.component.req.*;
import com.neusoft.weekreport.component.resp.GetWeekDetailInfoResp;
import com.neusoft.weekreport.component.resp.GetWeekSummaryResp;
import com.neusoft.weekreport.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class WeekBizServiceImpl implements WeekBizService {

    @Resource
    BsWeekDetailMapper bsWeekDetailMapper;
    @Resource
    ReWeekDetailMapper reWeekDetailMapper;
    @Resource
    BsWeekSummaryMapper bsWeekSummaryMapper;
    @Resource
    MsGroupRefUserMapper msGroupRefUserMapper;
    @Resource
    MsGroupMapper msGroupMapper;
    @Resource
    DcProductMapper dcProductMapper;
    @Resource
    MsUserMapper msUserMapper;


    //添加项目周报明细
    @Override
    @Transactional
    public void addProjectWeekDetaiList(GetAddProjectWorkListReq req) throws Exception {
        String userId = req.getUserId();
        String weekId = req.getWeekId();
        List<GetAddProjectWeekDetailReq> weekDetailList = req.getWeekDetailList();
        for (GetAddProjectWeekDetailReq weekDetail : weekDetailList) {
            ProjectWeekTaskInfo productInfo = weekDetail.getTaskInfo();
            BsWeekDetail bsWeekDetail = new BsWeekDetail();
            bsWeekDetail.setColid(UUID.randomUUID().toString());
            bsWeekDetail.setTaskId(productInfo.getCode());//任务id
            String NewTaskId = productInfo.getValue();
            bsWeekDetail.setWorkContent(filterWorkContent(weekDetail.getWorkContent()));//工作内容
            if(!StringUtils.isEmpty(weekDetail.getColid())){//删除原用
                BsWeekDetailExample bsWeekDetailExample = new BsWeekDetailExample();
                BsWeekDetailExample.Criteria criteria = bsWeekDetailExample.createCriteria();
                criteria.andColidEqualTo(weekDetail.getColid());
                criteria.andWeekidEqualTo(weekId);
                List<BsWeekDetail> bsWeekDetailList = bsWeekDetailMapper.selectByExample(bsWeekDetailExample);
                String taskId="";
                for (BsWeekDetail detail : bsWeekDetailList) {
                    taskId = detail.getTaskId();
                    break;
                }
                bsWeekDetailMapper.deleteByExample(bsWeekDetailExample);
                if(!NewTaskId.equals(taskId)){//如果修改的是任务，则判断是否删除周报概述
                    //判断是否删除周报概述
                    List<String> userIdList = new ArrayList<>();
                    for (WeekUserInfo userInfo : req.getUserInfoList()) {
                        userIdList.add(userInfo.getCode());
                    }
                    GetDeleteWeekReq getDeleteWeekReq = new GetDeleteWeekReq();
                    getDeleteWeekReq.setWeekId(req.getWeekId());
                    getDeleteWeekReq.setTaskId(taskId);
                    DeleteSummaryByDetail(getDeleteWeekReq,userIdList,req.getTaskType());
                }

            }
            for (WeekUserInfo userInfo : weekDetail.getUserInfo()) { //插入
                bsWeekDetail.setMainPersonId(userInfo.getCode());
                bsWeekDetail.setId(UUID.randomUUID().toString());
                bsWeekDetail.setWeekid(weekId);
                bsWeekDetail.setGroupId(userInfo.getGroupId());
                bsWeekDetail.setTaskType(req.getTaskType());
                bsWeekDetail.setCreateBy(userId);
                bsWeekDetail.setCreateDate(sysDateTime());
                bsWeekDetail.setUpdateBy(userId);
                bsWeekDetail.setUpdateDate(sysDateTime());
                bsWeekDetailMapper.insert(bsWeekDetail);
            }
        }
    }
    //添加研发周报
    public void addProductWeekDetaiList(GetAddProductWorkListReq req) throws Exception {
        String userId = req.getUserId();
        String weekId = req.getWeekId();
        List<GetAddProductWeekDetailReq> weekDetailList = req.getWeekDetailList();
        for (GetAddProductWeekDetailReq weekDetail : weekDetailList) {
            ProductWeekTaskInfo productInfo = weekDetail.getTaskInfo();
            BsWeekDetail bsWeekDetail = new BsWeekDetail();
            bsWeekDetail.setColid(UUID.randomUUID().toString());
            List<String> taskIDList = productInfo.getValue();
            String IdTask = taskIDList.get(taskIDList.size()-1);
            bsWeekDetail.setTaskId(IdTask);//任务 id
            String NewTaskId = IdTask;
            bsWeekDetail.setWorkContent(filterWorkContent(weekDetail.getWorkContent()));//工作内容
            if(!StringUtils.isEmpty(weekDetail.getColid())){//删除原有
                BsWeekDetailExample bsWeekDetailExample = new BsWeekDetailExample();
                BsWeekDetailExample.Criteria criteria = bsWeekDetailExample.createCriteria();
                criteria.andColidEqualTo(weekDetail.getColid());
                criteria.andWeekidEqualTo(weekId);
                List<BsWeekDetail> bsWeekDetailList = bsWeekDetailMapper.selectByExample(bsWeekDetailExample);
                String taskId="";
                for (BsWeekDetail detail : bsWeekDetailList) {
                    taskId = detail.getTaskId();
                    break;
                }
                bsWeekDetailMapper.deleteByExample(bsWeekDetailExample);
                if(!NewTaskId.equals(taskId)){//如果修改的是任务，则判断是否删除周报概述
                    //判断是否删除周报概述
                    List<String> userIdList = new ArrayList<>();
                    for (WeekUserInfo userInfo : req.getUserInfoList()) {
                        userIdList.add(userInfo.getCode());
                    }
                    GetDeleteWeekReq getDeleteWeekReq = new GetDeleteWeekReq();
                    getDeleteWeekReq.setWeekId(req.getWeekId());
                    getDeleteWeekReq.setTaskId(taskId);
                    DeleteSummaryByDetail(getDeleteWeekReq,userIdList,req.getTaskType());
                }
            }
            for (WeekUserInfo userInfo : weekDetail.getUserInfo()) { //插入
                bsWeekDetail.setMainPersonId(userInfo.getCode());
                bsWeekDetail.setId(UUID.randomUUID().toString());
                bsWeekDetail.setWeekid(weekId);
                bsWeekDetail.setGroupId(userInfo.getGroupId());
                bsWeekDetail.setTaskType(req.getTaskType());
                bsWeekDetail.setCreateBy(userId);
                bsWeekDetail.setCreateDate(sysDateTime());
                bsWeekDetail.setUpdateBy(userId);
                bsWeekDetail.setUpdateDate(sysDateTime());
                bsWeekDetailMapper.insert(bsWeekDetail);
            }
        }
    }

    //查询周报明细
    @Override
    public GetWeekDetailInfoResp getWeekDetailInfoList(GetWeekDetailListReq req) throws Exception{
        GetWeekDetailInfoResp resp = new GetWeekDetailInfoResp();

        List<WeekUserInfo> userInfoList = req.getUserIdList();
        List<String> userIdList = new ArrayList<>();
        for (WeekUserInfo weekUserInfo : userInfoList) {
            userIdList.add(weekUserInfo.getCode());
        }

        List<ReWeekDetail> queryProductWeekList =reWeekDetailMapper.selectProductWeekDetail(req.getWeekId(),userIdList,"1");
        List<ReWeekDetail> queryNextProductWeekList =reWeekDetailMapper.selectProductWeekDetail(req.getWeekId(),userIdList,"3");

        List<ReWeekDetail> queryProjectWeekList =reWeekDetailMapper.selectProjectWeekDetail(req.getWeekId(),userIdList,"2");
        List<ReWeekDetail> queryNextProjectWeekList =reWeekDetailMapper.selectProjectWeekDetail(req.getWeekId(),userIdList,"4");

        List<GetAddProductWeekDetailReq> productList = new ArrayList();
        List<GetAddProductWeekDetailReq> nextProductList = new ArrayList();
        List<GetAddProjectWeekDetailReq> projectList = new ArrayList();
        List<GetAddProjectWeekDetailReq> nextProjectList = new ArrayList();

        if(!CollectionUtils.isEmpty(queryProductWeekList)){
            for (ReWeekDetail queryProductWeekDetailResult : queryProductWeekList) {

                GetAddProductWeekDetailReq weekMap = new GetAddProductWeekDetailReq();
                //设置用户
                WeekUserInfo userInfo = new WeekUserInfo();
                userInfo.setCode(queryProductWeekDetailResult.getMainPersonId());
                userInfo.setValue(queryProductWeekDetailResult.getUserName());
                userInfo.setGroupId(queryProductWeekDetailResult.getGroupId());

                weekMap.setColid(queryProductWeekDetailResult.getColid());
                //设置工作内容
                weekMap.setWorkContent(queryProductWeekDetailResult.getWorkContent());
                //设置任务
                ProductWeekTaskInfo productWeekTaskInfo = new ProductWeekTaskInfo();
                String taskId = queryProductWeekDetailResult.getTaskId();
                DcProduct dcProduct =  dcProductMapper.selectByPrimaryKey(taskId);
                List<String> taskIdList = new ArrayList<>();
                taskIdList = getProductList(taskIdList,dcProduct.getParentId());
                taskIdList.add(taskId);
                productWeekTaskInfo.setValue(taskIdList);
                productWeekTaskInfo.setLabel(queryProductWeekDetailResult.getTaskName());
                weekMap.setTaskInfo(productWeekTaskInfo);
                weekMap.setEditSet(false);
                if(productList.size()==0){
                    List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                    weekUserInfoList.add(userInfo);
                    weekMap.setUserInfo(weekUserInfoList);
                    productList.add(weekMap);
                }else{
                    int count = 1;
                    for (GetAddProductWeekDetailReq weekDetailReq : productList) {
                        if(weekDetailReq.getColid().equals(queryProductWeekDetailResult.getColid())){
                            List<WeekUserInfo> weekUserInfoList = weekDetailReq.getUserInfo();
                            weekUserInfoList.add(userInfo);
                            weekDetailReq.setUserInfo(weekUserInfoList);
                            break;
                        }else{
                            if(productList.size()==count){
                                List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                                weekUserInfoList.add(userInfo);
                                weekMap.setUserInfo(weekUserInfoList);
                                productList.add(weekMap);
                                break;
                            }else{
                                ++count;
                            }
                        }
                    }
                }
            }
        }
        if(!CollectionUtils.isEmpty(queryNextProductWeekList)){
            for (ReWeekDetail queryNextProductWeekResult : queryNextProductWeekList) {

                GetAddProductWeekDetailReq weekMap = new GetAddProductWeekDetailReq();
                //设置用户
                WeekUserInfo userInfo = new WeekUserInfo();
                userInfo.setCode(queryNextProductWeekResult.getMainPersonId());
                userInfo.setValue(queryNextProductWeekResult.getUserName());
                userInfo.setGroupId(queryNextProductWeekResult.getGroupId());

                weekMap.setColid(queryNextProductWeekResult.getColid());
                //设置工作内容
                weekMap.setWorkContent(queryNextProductWeekResult.getWorkContent());

                //设置任务
                ProductWeekTaskInfo productWeekTaskInfo = new ProductWeekTaskInfo();
                String taskId = queryNextProductWeekResult.getTaskId();
                DcProduct dcProduct =  dcProductMapper.selectByPrimaryKey(taskId);
                List<String> taskIdList = new ArrayList<>();
                taskIdList = getProductList(taskIdList,dcProduct.getParentId());
                taskIdList.add(taskId);
                productWeekTaskInfo.setValue(taskIdList);
                productWeekTaskInfo.setLabel(queryNextProductWeekResult.getTaskName());
                weekMap.setTaskInfo(productWeekTaskInfo);

                weekMap.setEditSet(false);
                if(nextProductList.size()==0){
                    List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                    weekUserInfoList.add(userInfo);
                    weekMap.setUserInfo(weekUserInfoList);
                    nextProductList.add(weekMap);
                }else{
                    int count = 1;
                    for (GetAddProductWeekDetailReq weekDetailReq : nextProductList) {
                        if(weekDetailReq.getColid().equals(queryNextProductWeekResult.getColid())){
                            List<WeekUserInfo> weekUserInfoList = weekDetailReq.getUserInfo();
                            weekUserInfoList.add(userInfo);
                            weekDetailReq.setUserInfo(weekUserInfoList);
                            break;
                        }else{
                            if(nextProductList.size()==count){
                                List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                                weekUserInfoList.add(userInfo);
                                weekMap.setUserInfo(weekUserInfoList);
                                nextProductList.add(weekMap);
                                break;
                            }else{
                                ++count;
                            }
                        }
                    }
                }
            }
        }
        if(!CollectionUtils.isEmpty(queryProjectWeekList)){
            for (ReWeekDetail queryProjectWeekResult : queryProjectWeekList) {

                GetAddProjectWeekDetailReq weekMap = new GetAddProjectWeekDetailReq();
                //设置用户
                WeekUserInfo userInfo = new WeekUserInfo();
                userInfo.setCode(queryProjectWeekResult.getMainPersonId());
                userInfo.setValue(queryProjectWeekResult.getUserName());
                userInfo.setGroupId(queryProjectWeekResult.getGroupId());

                weekMap.setColid(queryProjectWeekResult.getColid());
                //设置工作内容
                weekMap.setWorkContent(queryProjectWeekResult.getWorkContent());
                //设置任务
                ProjectWeekTaskInfo projectWeekTaskInfo = new ProjectWeekTaskInfo();
                projectWeekTaskInfo.setCode(queryProjectWeekResult.getTaskId());
                projectWeekTaskInfo.setValue(queryProjectWeekResult.getTaskName());
                weekMap.setTaskInfo(projectWeekTaskInfo);
                weekMap.setEditSet(false);
                if(projectList.size()==0){
                    List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                    weekUserInfoList.add(userInfo);
                    weekMap.setUserInfo(weekUserInfoList);
                    projectList.add(weekMap);
                }else{
                    int count = 1;
                    for (GetAddProjectWeekDetailReq weekDetailReq : projectList) {
                        if(weekDetailReq.getColid().equals(queryProjectWeekResult.getColid())){
                            List<WeekUserInfo> weekUserInfoList = weekDetailReq.getUserInfo();
                            weekUserInfoList.add(userInfo);
                            weekDetailReq.setUserInfo(weekUserInfoList);
                            break;
                        }else{
                            if(projectList.size()==count){
                                List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                                weekUserInfoList.add(userInfo);
                                weekMap.setUserInfo(weekUserInfoList);
                                projectList.add(weekMap);
                                break;
                            }else{
                                ++count;
                            }
                        }
                    }
                }
            }

        }
        if(!CollectionUtils.isEmpty(queryNextProjectWeekList)){
            for (ReWeekDetail queryNextProjectWeekResult : queryNextProjectWeekList) {

                GetAddProjectWeekDetailReq weekMap = new GetAddProjectWeekDetailReq();
                //设置用户
                WeekUserInfo userInfo = new WeekUserInfo();
                userInfo.setCode(queryNextProjectWeekResult.getMainPersonId());
                userInfo.setValue(queryNextProjectWeekResult.getUserName());
                userInfo.setGroupId(queryNextProjectWeekResult.getGroupId());

                weekMap.setColid(queryNextProjectWeekResult.getColid());
                //设置工作内容
                weekMap.setWorkContent(queryNextProjectWeekResult.getWorkContent());
                //设置任务
                ProjectWeekTaskInfo projectWeekTaskInfo = new ProjectWeekTaskInfo();
                projectWeekTaskInfo.setCode(queryNextProjectWeekResult.getTaskId());
                projectWeekTaskInfo.setValue(queryNextProjectWeekResult.getTaskName());
                weekMap.setTaskInfo(projectWeekTaskInfo);
                weekMap.setEditSet(false);
                if(nextProjectList.size()==0){
                    List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                    weekUserInfoList.add(userInfo);
                    weekMap.setUserInfo(weekUserInfoList);
                    nextProjectList.add(weekMap);
                }else{
                    int count = 1;
                    for (GetAddProjectWeekDetailReq weekDetailReq : nextProjectList) {
                        if(weekDetailReq.getColid().equals(queryNextProjectWeekResult.getColid())){
                            List<WeekUserInfo> weekUserInfoList = weekDetailReq.getUserInfo();
                            weekUserInfoList.add(userInfo);
                            weekDetailReq.setUserInfo(weekUserInfoList);
                            break;
                        }else{
                            if(nextProjectList.size()==count){
                                List<WeekUserInfo> weekUserInfoList = new ArrayList<>();
                                weekUserInfoList.add(userInfo);
                                weekMap.setUserInfo(weekUserInfoList);
                                nextProjectList.add(weekMap);
                                break;
                            }else{
                                ++count;
                            }
                        }
                    }
                }
            }
        }

        resp.setProductWeekDetailList(productList);
        resp.setProjectWeekDetailList(projectList);
        resp.setNextProductWeekDetailList(nextProductList);
        resp.setNextProjectWeekDetailList(nextProjectList);

        return resp;
    }

    //递归查询产品
    public List<String> getProductList(List<String> productIdList,String taskId){
        DcProductExample dcProductExample = new DcProductExample();
        DcProductExample.Criteria criteria = dcProductExample.createCriteria();
        criteria.andIdEqualTo(taskId);
        List<DcProduct> dcProductList = dcProductMapper.selectByExample(dcProductExample);
        if(!CollectionUtils.isEmpty(dcProductList)){
            for (DcProduct dcProduct : dcProductList) {
                if(dcProduct.getParentId().equals("-1")){
                    productIdList.add(dcProduct.getId());
                }else {
                    productIdList.add(dcProduct.getId());
                    getProductList(productIdList,dcProduct.getParentId());
                }
            }
        }else{
            productIdList.add(taskId);
        }
        return productIdList;

    }

    //删除研发任务周报
    @Override
    @Transactional
    public void deleteWeekInfo(GetDeleteWeekReq req) throws Exception {
        //删除周报中的一条
        List<String> userIdList = new ArrayList<>();
        for (WeekUserInfo userInfo : req.getUserInfoList()) {
            userIdList.add(userInfo.getCode());
        }
        BsWeekDetailExample bsWeekDetailExample1 = new BsWeekDetailExample();
        BsWeekDetailExample.Criteria criteria1 = bsWeekDetailExample1.createCriteria();
        criteria1.andWeekidEqualTo(req.getWeekId());
        criteria1.andColidEqualTo(req.getColid());
        criteria1.andMainPersonIdIn(userIdList);
        bsWeekDetailMapper.deleteByExample(bsWeekDetailExample1);
        //查询出删除的周报负责人所属组
        MsGroupRefUserExample msGroupRefUserExample = new MsGroupRefUserExample();
        MsGroupRefUserExample.Criteria criteria3 = msGroupRefUserExample.createCriteria();
        criteria3.andUseridIn(userIdList);
        List<MsGroupRefUser> msGroupRefUserList = msGroupRefUserMapper.selectByExample(msGroupRefUserExample);
        List<String> groupIdList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(msGroupRefUserList)){
            for (MsGroupRefUser msGroupRefUser : msGroupRefUserList) {
                groupIdList.add(msGroupRefUser.getGroupid());
            }
            //查询出所属组下的所有用户
            msGroupRefUserExample.clear();
            criteria3 = msGroupRefUserExample.createCriteria();
            criteria3.andGroupidIn(groupIdList);
            List<MsGroupRefUser> msGroupRefUserList1 = msGroupRefUserMapper.selectByExample(msGroupRefUserExample);
            List<String> userIdList1 = new ArrayList<>();
            for (MsGroupRefUser refUser : msGroupRefUserList1) {
                userIdList1.add(refUser.getUserid());
            }
            //查询用户所在组下的任务id和删除的任务id一致的所有周报
            BsWeekDetailExample bsWeekDetailExample = new BsWeekDetailExample();
            BsWeekDetailExample.Criteria criteria = bsWeekDetailExample.createCriteria();
            criteria.andWeekidEqualTo(req.getWeekId());
            criteria.andMainPersonIdIn(userIdList1);
            criteria.andTaskIdEqualTo(req.getTaskId());
            List<BsWeekDetail> bsWeekDetailList = bsWeekDetailMapper.selectByExample(bsWeekDetailExample);

            //如果没同一任务的周报，就可以删除同一任务的概述
            if(CollectionUtils.isEmpty(bsWeekDetailList)){
                BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
                BsWeekSummaryExample.Criteria criteria2 = bsWeekSummaryExample.createCriteria();
                criteria2.andWeekidEqualTo(req.getWeekId());
                criteria2.andTaskIdEqualTo(req.getTaskId());
                criteria2.andGroupIdIn(groupIdList);
                bsWeekSummaryMapper.deleteByExample(bsWeekSummaryExample);
            }
        }
    }

    //切换周报明细任务时，判断是否删除对应周报概述
    public void DeleteSummaryByDetail(GetDeleteWeekReq req,List<String> userIdList,String taskType){
        //查询出删除的周报负责人所属组
        MsGroupRefUserExample msGroupRefUserExample = new MsGroupRefUserExample();
        MsGroupRefUserExample.Criteria criteria3 = msGroupRefUserExample.createCriteria();
        criteria3.andUseridIn(userIdList);
        List<MsGroupRefUser> msGroupRefUserList = msGroupRefUserMapper.selectByExample(msGroupRefUserExample);
        List<String> groupIdList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(msGroupRefUserList)){
            for (MsGroupRefUser msGroupRefUser : msGroupRefUserList) {
                groupIdList.add(msGroupRefUser.getGroupid());
            }
            //查询出所属组下的所有用户
            msGroupRefUserExample.clear();
            criteria3 = msGroupRefUserExample.createCriteria();
            criteria3.andGroupidIn(groupIdList);
            List<MsGroupRefUser> msGroupRefUserList1 = msGroupRefUserMapper.selectByExample(msGroupRefUserExample);
            List<String> userIdList1 = new ArrayList<>();
            for (MsGroupRefUser refUser : msGroupRefUserList1) {
                userIdList1.add(refUser.getUserid());
            }
            //查询用户所在组下的任务id和删除的任务id一致的所有周报
            BsWeekDetailExample bsWeekDetailExample = new BsWeekDetailExample();
            BsWeekDetailExample.Criteria criteria = bsWeekDetailExample.createCriteria();
            criteria.andWeekidEqualTo(req.getWeekId());
            criteria.andMainPersonIdIn(userIdList1);
            criteria.andTaskIdEqualTo(req.getTaskId());
            criteria.andTaskTypeEqualTo(taskType);
            List<BsWeekDetail> bsWeekDetailList = bsWeekDetailMapper.selectByExample(bsWeekDetailExample);

            //如果没同一任务的周报，就可以删除同一任务的概述
            if(CollectionUtils.isEmpty(bsWeekDetailList)){
                BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
                BsWeekSummaryExample.Criteria criteria2 = bsWeekSummaryExample.createCriteria();
                criteria2.andWeekidEqualTo(req.getWeekId());
                criteria2.andTaskIdEqualTo(req.getTaskId());
                criteria2.andGroupIdIn(groupIdList);
                bsWeekSummaryMapper.deleteByExample(bsWeekSummaryExample);
            }
        }
    }

    //@提交本周周报概述
    @Override
    @Transactional
    public void submitWeekSummaryInfo(GetAddWeekSummaryReq req) throws Exception{

        //提交研发概述
        for (productWeekSummaryInfo weekSummaryInfo : req.getProductWeek()) {
                BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
                BsWeekSummaryExample.Criteria criteria = bsWeekSummaryExample.createCriteria();
                criteria.andWeekidEqualTo(req.getWeekId());
                criteria.andTaskIdEqualTo(weekSummaryInfo.getTaskId());
                List<BsWeekSummary> bsWeekSummaryList = bsWeekSummaryMapper.selectByExample(bsWeekSummaryExample);
                BsWeekSummary bsWeekSummary = new BsWeekSummary();
                if(StringUtils.isEmpty(weekSummaryInfo.getWorkSummary())){
                    bsWeekSummary.setWorkContent("");
                }else{
                    bsWeekSummary.setWorkContent(weekSummaryInfo.getWorkSummary());
                }
                bsWeekSummary.setUpdateDate(sysDateTime());
                bsWeekSummary.setUpdateBy(req.getUserId());
                bsWeekSummary.setId(UUID.randomUUID().toString());
                bsWeekSummary.setTaskId(weekSummaryInfo.getTaskId());
                bsWeekSummary.setWeekid(req.getWeekId());
                bsWeekSummary.setTaskType("1");
                if(CollectionUtils.isEmpty(bsWeekSummaryList)){
                    bsWeekSummary.setTaskType("1");
                    bsWeekSummary.setCreateDate(sysDateTime());
                    bsWeekSummary.setCreateBy(req.getUserId());
                    bsWeekSummaryMapper.insert(bsWeekSummary);
                }else{
                    bsWeekSummaryMapper.updateByExampleSelective(bsWeekSummary,bsWeekSummaryExample);
                }


        }

        //提交项目支持概述
        for (weekSummaryInfo weekSummaryInfo : req.getProjectWeek()) {
            for (workSummary workSummary : weekSummaryInfo.getWorkSummaryList()) {
                BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
                BsWeekSummaryExample.Criteria criteria = bsWeekSummaryExample.createCriteria();
                criteria.andWeekidEqualTo(req.getWeekId());
                criteria.andTaskIdEqualTo(weekSummaryInfo.getTaskId());
                criteria.andGroupIdEqualTo(workSummary.getGroupId());
                List<BsWeekSummary> bsWeekSummaryList=bsWeekSummaryMapper.selectByExample(bsWeekSummaryExample);
                if(!CollectionUtils.isEmpty(bsWeekSummaryList)){//修改
                    BsWeekSummary bsWeekSummary = new BsWeekSummary();
                    bsWeekSummary.setGroupId(workSummary.getGroupId());
                    bsWeekSummary.setWorkContent(workSummary.getWorkContent());
                    bsWeekSummary.setUpdateDate(sysDateTime());
                    bsWeekSummary.setUpdateBy(req.getUserId());
                    bsWeekSummaryMapper.updateByExampleSelective(bsWeekSummary,bsWeekSummaryExample);
                }else{//添加
                    BsWeekSummary bsWeekSummary = new BsWeekSummary();
                    bsWeekSummary.setId(UUID.randomUUID().toString());
                    bsWeekSummary.setTaskId(weekSummaryInfo.getTaskId());
                    bsWeekSummary.setWeekid(req.getWeekId());
                    bsWeekSummary.setGroupId(workSummary.getGroupId());
                    bsWeekSummary.setWorkContent(workSummary.getWorkContent());
                    bsWeekSummary.setTaskType("2");
                    bsWeekSummary.setCreateDate(sysDateTime());
                    bsWeekSummary.setUpdateDate(sysDateTime());
                    bsWeekSummary.setCreateBy(req.getUserId());
                    bsWeekSummary.setUpdateBy(req.getUserId());
                    bsWeekSummaryMapper.insert(bsWeekSummary);
                }
            }
        }
    }


    //查询本周概述
    @Override
    public GetWeekSummaryResp getWeekSummaryInfoList(GetWeekSummaryReq req) throws Exception{
        GetWeekSummaryResp resp = new GetWeekSummaryResp();
        //查询出本周周报明细
        List<productWeekSummaryInfo> productWeekSummaryInfoList = new ArrayList<>();
        BsWeekDetailExample bsWeekDetailExample = new BsWeekDetailExample();
        BsWeekDetailExample.Criteria criteria9 = bsWeekDetailExample.createCriteria();
        criteria9.andWeekidEqualTo(req.getWeekId());
        criteria9.andTaskTypeEqualTo("1");
        bsWeekDetailExample.setOrderByClause("UPDATE_DATE");
        List<BsWeekDetail> bsWeekDetailList =bsWeekDetailMapper.selectByExample(bsWeekDetailExample);
        //过滤出
        if(!CollectionUtils.isEmpty(bsWeekDetailList)){
            List<DcProduct> hashSetList = new ArrayList<>();
            if(req.getRoleId().equals("10004") || req.getRoleId().equals("10001")){
                List<String> productIdList = new ArrayList<>();
                for (BsWeekDetail bsWeekDetail : bsWeekDetailList) {
                    productIdList.add(bsWeekDetail.getTaskId());
                }
                DcProductExample dcProductExample = new DcProductExample();
                DcProductExample.Criteria criteria11 = dcProductExample.createCriteria();
                criteria11.andIdIn(productIdList);
                criteria11.andEnableEqualTo(1);
                dcProductExample.setOrderByClause("ORDER_NUM");
                dcProductExample.setOrderByClause("PRODUCT_NAME");
                List<DcProduct> dcProductList = dcProductMapper.selectByExample(dcProductExample);
                hashSetList = dcProductList;
            }else{
                List<DcProduct> dcProductList = req.getMainProductList();
                for (DcProduct dcProduct : dcProductList) {
                    for (BsWeekDetail bsWeekDetail : bsWeekDetailList) {
                        if(bsWeekDetail.getTaskId().equals(dcProduct.getId())){
                            hashSetList.add(dcProduct);
                            break;
                        }else{
                            continue;
                        }
                    }
                }
            }
            int num=1;
            if(!CollectionUtils.isEmpty(hashSetList)){

                for (DcProduct dcProduct : hashSetList) {
                    productWeekSummaryInfo productWeekSummaryInfo = new productWeekSummaryInfo();
                    productWeekSummaryInfo.setNum(Integer.toString(num)+"、");
                    if(StringUtils.isEmpty(dcProduct.getUserId())){
                        productWeekSummaryInfo.setUserName("XXX");
                    }else{
                        MsUser msUser = msUserMapper.selectByPrimaryKey(dcProduct.getUserId());
                        productWeekSummaryInfo.setUserName(msUser.getUserName());
                    }

                    productWeekSummaryInfo.setTaskId(dcProduct.getId());
                    productWeekSummaryInfo.setTaskName(dcProduct.getProductName());
                    productWeekSummaryInfo.setUserId(dcProduct.getUserId());
                    productWeekSummaryInfo.setEditSet(req.getEditSet());


                    BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
                    BsWeekSummaryExample.Criteria criteria8= bsWeekSummaryExample.createCriteria();
                    criteria8.andWeekidEqualTo(req.getWeekId());
                    criteria8.andTaskIdEqualTo(dcProduct.getId());
                    List<BsWeekSummary> bsWeekSummaryList = bsWeekSummaryMapper.selectByExample(bsWeekSummaryExample);
                    if(!CollectionUtils.isEmpty(bsWeekDetailList)){
                        String summary="";
                        for (BsWeekSummary bsWeekSummary : bsWeekSummaryList) {
                            summary +=bsWeekSummary.getWorkContent();
                        }
                        productWeekSummaryInfo.setWorkSummary(summary);
                    }else{
                        productWeekSummaryInfo.setWorkSummary("");
                    }
                    productWeekSummaryInfoList.add(productWeekSummaryInfo);
                    ++num;
                }

            }
        }
        resp.setProductWeek(productWeekSummaryInfoList);
//        List<weekSummaryInfo> list = new ArrayList<>();
//        int num=1;
//        for (weekSummaryInfo weekSummaryInfo : req.getProductWeek()) {//研发概述
//            List<workSummary> workSummaryList = new ArrayList<>();
//            for (workSummary workSummary : weekSummaryInfo.getWorkSummaryList()) {
//                BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
//                BsWeekSummaryExample.Criteria criteria = bsWeekSummaryExample.createCriteria();
//                criteria.andWeekidEqualTo(req.getWeekId());
//                criteria.andTaskIdEqualTo(weekSummaryInfo.getTaskId());
//                criteria.andGroupIdEqualTo(workSummary.getGroupId());
//                List<BsWeekSummary> bsWeekSummaryList = bsWeekSummaryMapper.selectByExample(bsWeekSummaryExample);
//                workSummary ws = new workSummary();
//                if(CollectionUtils.isEmpty(bsWeekSummaryList)){
//                    ws.setGroupId(workSummary.getGroupId());
//                    ws.setGroupName(getGroupName(workSummary.getGroupId()));
//
//                    ws.setWorkContent("");
//                }else{
//                    String workContent="";
//                    for (BsWeekSummary bsWeekSummary : bsWeekSummaryList) {
//                        workContent = bsWeekSummary.getWorkContent();
//                        break;
//                    }
//                    ws.setGroupName(getGroupName(workSummary.getGroupId()));
//                    ws.setGroupId(workSummary.getGroupId());
//                    ws.setWorkContent(workContent);
//                }
//                workSummaryList.add(ws);
//            }
//            DcProduct dcProduct = dcProductMapper.selectByPrimaryKey(weekSummaryInfo.getTaskId());
//            if(!StringUtils.isEmpty(dcProduct.getUserId())){
//                MsUser msUser = msUserMapper.selectByPrimaryKey(dcProduct.getUserId());
//                weekSummaryInfo.setUserName(msUser.getUserName());
//            }else{
//                weekSummaryInfo.setUserName("无");
//            }
//
//            weekSummaryInfo.setNum(Integer.toString(num)+"、");
//            weekSummaryInfo.setTaskName(weekSummaryInfo.getTaskName());
//            weekSummaryInfo.setUserId(dcProduct.getUserId());
//
//            weekSummaryInfo.setWorkSummaryList(workSummaryList);
//            weekSummaryInfo.setEditSet(req.getEditSet());
//            list.add(weekSummaryInfo);
//            ++num;
//        }
//        resp.setProductWeek(list);
        List<weekSummaryInfo> list1 = new ArrayList<>();

        int num1=1;
        for (weekSummaryInfo weekSummaryInfo : req.getProjectWeek()) {//项目支持概述
            List<workSummary> workSummaryList = new ArrayList<>();
            for (workSummary workSummary : weekSummaryInfo.getWorkSummaryList()) {
                BsWeekSummaryExample bsWeekSummaryExample = new BsWeekSummaryExample();
                BsWeekSummaryExample.Criteria criteria = bsWeekSummaryExample.createCriteria();
                criteria.andWeekidEqualTo(req.getWeekId());
                criteria.andTaskIdEqualTo(weekSummaryInfo.getTaskId());
                criteria.andGroupIdEqualTo(workSummary.getGroupId());
                List<BsWeekSummary> bsWeekSummaryList = bsWeekSummaryMapper.selectByExample(bsWeekSummaryExample);
                workSummary ws = new workSummary();
                if(CollectionUtils.isEmpty(bsWeekSummaryList)){
                    ws.setGroupId(workSummary.getGroupId());
                    ws.setGroupName(getGroupName(workSummary.getGroupId()));
                    ws.setWorkContent("");
                }else{
                    String workContent="";
                    for (BsWeekSummary bsWeekSummary : bsWeekSummaryList) {
                        workContent = bsWeekSummary.getWorkContent();
                        break;
                    }
                    ws.setGroupId(workSummary.getGroupId());
                    ws.setGroupName(getGroupName(workSummary.getGroupId()));
                    ws.setWorkContent(workContent);
                }
                workSummaryList.add(ws);
            }
            weekSummaryInfo.setNum(Integer.toString(num1)+"、");
            weekSummaryInfo.setTaskName(weekSummaryInfo.getTaskName());
            weekSummaryInfo.setWorkSummaryList(workSummaryList);
            weekSummaryInfo.setEditSet(req.getEditSet());
            list1.add(weekSummaryInfo);
            ++num1;
        }
        resp.setProjectWeek(list1);
        return  resp;
    }

    public String getGroupName(String groupId){
        MsGroup msGroup = msGroupMapper.selectByPrimaryKey(groupId);
        return msGroup.getGroupName();
    }

    //处理编号
    public String filterWorkContent(String workContent){
        String newWorkContent = (Pattern.compile(("^[0-9]+[、]|[0-9]+[.]|[0-9]+[\\\\]"),Pattern.MULTILINE)).matcher(workContent).replaceAll("");
        return newWorkContent;
    }


    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }
}
